///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_BBOX_DETAIL_CMETHOD_H_
#define _BUOLA_BBOX_DETAIL_CMETHOD_H_

#include <buola/bbox/protocol/cpacket.h>
#include <buola/traits/tuple.h>

namespace buola { namespace bbox { namespace detail {
    
class CMethodBase
{
public:
    virtual ~CMethodBase() {};
    virtual void Dispatch(protocol::CPacket &pP,protocol::CPacket &pR,void *pO)=0;
};

template<typename tSignature,typename tSub>
class CMethod;

template<typename tReturn,typename... tArgs,typename tSub>
class CMethod<tReturn(tArgs...),tSub> : public CMethodBase
{
public:
    typedef std::function<tReturn(tSub*,tArgs...)> tComplete;
    typedef std::tuple<ODecay<tArgs>...> tArgsTuple;

    CMethod(const tComplete &pF)
        :   mF(pF)
    {}

    template<typename tRet,std::size_t... pIndices>
    OEnableIf<std::is_same<void,tRet>,void>
    MakeFunctionCall(protocol::CPacket &pPacket,protocol::CPacket &pR,
                     void *pO,const MIndexTuple<pIndices...>&,const tArgsTuple &pArgs)
    {
        mF((tSub*)pO,std::get<pIndices>(pArgs)...);
    }
    
    template<typename tRet,std::size_t... pIndices>
    OEnableIfNot<std::is_same<void,tRet>,void>
    MakeFunctionCall(protocol::CPacket &pPacket,protocol::CPacket &pR,
                     void *pO,const MIndexTuple<pIndices...>&,const tArgsTuple &pArgs)
    {
        tReturn lRet=mF((tSub*)pO,std::get<pIndices>(pArgs)...);
        pR << lRet;
    }

public:
    void Dispatch(protocol::CPacket &pPacket,protocol::CPacket &pReply,void *pO) final
    {
        tArgsTuple lTuple;
        pPacket >> lTuple;
        typedef typename MMakeIndices<sizeof...(tArgs)>::type tIndices; 
        MakeFunctionCall<tReturn>(pPacket,pReply,pO,tIndices(),lTuple);
    }

    tComplete mF;
};

/*namespace detail*/ } /*namespace bbox*/ } /*namespace buola*/ }

#endif
